package io.github.biezhi.elves.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;

import io.github.biezhi.elves.bidding.model.BiddingDTO;

/**
 * 解析文档工具类
 *
 */
public class ResolveDocument {
	/**
	 * 读取word文档
	 * @param filePath
	 * @return
	 */
	@SuppressWarnings("resource")
	public static String getTextFromWord(String filePath) {
		String result = "";
        try {
            FileInputStream fis = new FileInputStream(filePath);
            HWPFDocument doc = new HWPFDocument(fis);
            result = doc.getDocumentText();
            doc.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
		return result;
	}
	
	/**
	 * 读取pdf数据
	 * @param filePath
	 * @return
	 */
	public static String getTextFromPdf(String filePath) {
		String result = null;
		FileInputStream is = null;
		PDDocument document = null;
		try {
			is = new FileInputStream(filePath);
			PDFParser parser = new PDFParser(is);
			parser.parse();
			document = parser.getPDDocument();
			PDFTextStripper stripper = new PDFTextStripper();
			result = stripper.getText(document);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (document != null) {
				try {
					document.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		String downUrl = "http://www.gdgpo.gov.cn/gdgpms/upload/files/article_attachment/2015/8/13/1439451037671.doc";
		String filePath = "f:/1439451037671.doc";
		String wordText = getTextFromWord(filePath);
		BiddingDTO bidding = new BiddingDTO();
		bidding.setBidder(StringUtils.matchTargetContent(wordText));
		bidding.setBiddingTime(StringUtils.regexDateByHtml(wordText));
		bidding.setBiddingPrice(StringUtils.matchProjectPrice(wordText));
		System.out.println(bidding);
	}
}